//
//  DMAPIProxy.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/8/1.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "DMAPIProxy.h"
#import <AFNetworking/AFNetworking.h>
#import "DMBaseAPIManager.h"
#import "NSURLRequest+DMNetworking.h"

@interface DMAPIProxy()
@property (nonatomic, strong) NSMutableDictionary *dispatchTable;
@property (nonatomic, strong) NSNumber *recordedRequestId;
@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;

@end
@implementation DMAPIProxy

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static DMAPIProxy *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (NSInteger)loadGETWithParams:(NSDictionary *)params
                       useJSON:(BOOL)useJSON
                          host:(NSString *)host
                          path:(NSString *)path
                    apiVersion:(NSString *)version
                       success:(DMAPIProxySuccess)success
                          fail:(DMAPIProxyFail)fail {
    NSURLRequest *request = [DMAPIProxy requestWithParams:params
                                                  useJSON:useJSON
                                                   method:@"GET"
                                                     host:host
                                                     path:path
                                               apiVersion:version];
    
    NSNumber *requestId = [self loadRequest:request success:success fail:fail];
    return [requestId integerValue];
}

- (NSInteger)loadPOSTWithParams:(NSDictionary *)params
                        useJSON:(BOOL)useJSON
                           host:(NSString *)host
                           path:(NSString *)path
                     apiVersion:(NSString *)version
                        success:(DMAPIProxySuccess)success
                           fail:(DMAPIProxyFail)fail {
    NSURLRequest *request = [DMAPIProxy requestWithParams:params
                                                  useJSON:useJSON
                                                   method:@"POST"
                                                     host:host
                                                     path:path
                                               apiVersion:version];
    NSNumber *requestId = [self loadRequest:request success:success fail:fail];
    return [requestId integerValue];
}

- (void)cancelRequestWithRequestId:(NSNumber *)requestID {
    NSURLSessionDataTask *requestOperation = self.dispatchTable[requestID];
    [requestOperation cancel];
    [self.dispatchTable removeObjectForKey:requestID];
}

- (void)cancelRequestWithRequestIdList:(NSArray *)requestIDList {
    for (NSNumber *requestId in requestIDList) {
        [self cancelRequestWithRequestId:requestId];
    }
}


- (NSNumber *)loadRequest:(NSURLRequest *)request
                  success:(DMAPIProxySuccess)success
                     fail:(DMAPIProxyFail)fail {
    NSLog(@"%@",request.HTTPBody);
    __block NSURLSessionDataTask *dataTask = nil;
    
    
    dataTask = [self.sessionManager dataTaskWithRequest:request
                                      completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        NSNumber *requestID = @([dataTask taskIdentifier]);
        [self.dispatchTable removeObjectForKey:requestID];
        NSData *responseData = nil;
        if ([responseObject isKindOfClass:[NSData class]]) {
            responseData = responseObject;
        }
        if (error) {
            NSLog(@"网络错误:%@",error);
            if (error.code == NSURLErrorTimedOut) {
                fail?fail(DMResponseError(@"网络超时",DMResponseStatusErrorTimeout,[requestID integerValue])):nil;
            } else {
                fail?fail(DMResponseError(@"网络错误",DMResponseStatusErrorUnknown,[requestID integerValue])):nil;
            }
        } else {
            NSString *responseString = nil;
            if (responseData != nil) {
                responseString = [[NSString alloc] initWithData:responseData
                                                       encoding:NSUTF8StringEncoding];;
            }
            
            DMResponseModel *responseModel =
            [[DMResponseModel alloc] initWithResponseString:responseString
                                                  requestId:[requestID integerValue]
                                                    request:request
                                               responseData:responseData
                                                     status:DMResponseStatusSuccess];
            success?success(responseModel):nil;
        }
    }];
    
    NSNumber *requestId = @([dataTask taskIdentifier]);
    self.dispatchTable[requestId] = dataTask;
    [dataTask resume];
    return requestId;
}

- (BOOL)isReachable {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusUnknown) {
        return YES;
    } else {
        return [[AFNetworkReachabilityManager sharedManager] isReachable];
    }
}

#pragma mark - Getter

- (NSMutableDictionary *)dispatchTable {
    if (_dispatchTable == nil) {
        _dispatchTable = [[NSMutableDictionary alloc] init];
    }
    return _dispatchTable;
}

- (AFHTTPSessionManager *)sessionManager {
    if (_sessionManager == nil) {
        _sessionManager = [AFHTTPSessionManager manager];
        _sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
//        _sessionManager.securityPolicy.allowInvalidCertificates = YES;
//        _sessionManager.securityPolicy.validatesDomainName = NO;
    }
    return _sessionManager;
}
@end

@implementation DMAPIProxy (DMRequestGenerator)
+ (NSURLRequest *)requestWithParams:(NSDictionary *)params
                            useJSON:(BOOL)useJSON
                             method:(NSString *)method
                               host:(NSString *)host
                               path:(NSString *)path
                         apiVersion:(NSString *)version {
    NSString *urlString;
    if (version.length != 0) {
        urlString = [NSString stringWithFormat:@"%@/%@/%@",host, version, path];
    } else {
        urlString = [NSString stringWithFormat:@"%@/%@",host, path];
    }
    
    NSError *error = nil;
    if (![method isEqualToString:@"GET"]
        && ![method isEqualToString:@"POST"]) {
        DMLog(@"[DMAPIProxy]未知请求方法");
        return nil;
    }
    NSLog(@"%@",params);
    
    AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
    serializer.timeoutInterval = kDMTimeoutSeconds;
    NSURLRequest *request = [serializer requestWithMethod:method
                                                URLString:urlString
                                               parameters:params
                                                    error:&error];
    
    request.dm_requestParams = params;
    
    if (error) {
        NSLog(@"REQUEST ERROR:%@",request);
        return nil;
    }
    return request;
}
@end

